<?php

namespace System\Data\Drivers\MySQLi;

/**
 * Description of Result
 *
 * @author elton.schivei
 */
class Result extends \System\Types\Object implements \System\Data\IDataResult
{

    /**
     * @var \mysqli_result
     */
    private $_result;

    /**
     * @var \System\Data\Drivers\MySQLi\Driver
     */
    private $_driver;

    function __construct(\mysqli_result &$result, \System\Data\Drivers\MySQLi\Driver &$driver)
    {
        $this->_result = $result;
        $this->_driver = $driver;
    }

    public function &Fetch($type = \System\Data\IDataResult::FETCH_OBJECT, $orientation = \System\Data\IDataResult::FETCH_ROWS, $object = null, Array $params = array())
    {
        if (!$object)
        {
            $object = "";
        }

        $fetched = array();

        if ($orientation === self::FETCH_ROWS)
        {
            while ($row = (($type === self::FETCH_OBJECT) ?
                    (($object) ?
                            $this->_result->fetch_object($object, $params) :
                            $this->_result->fetch_object()) :
                    $this->_result->fetch_array(\MYSQLI_BOTH)))
            {
                $fetched[] = $row;
            }
        }
        else
        {
            $fetched = $this->_result->fetch_fields();
        }

        $this->_result->free_result();

        return $fetched;
    }

    public function &FetchUnique($index = 0, $type = \System\Data\IDataResult::FETCH_OBJECT, $object = null, Array $params = array())
    {
        $fetches = $this->Fetch($type, self::FETCH_ROWS, $object, $params);

        $fetch = null;

        if ($index < \count($fetches))
            $fetch = $fetches[$index];

        return $fetch;
    }

    public function NumRows()
    {
        return $this->_result->num_rows;
    }

}
